import {$http} from '@bana/miniapp-common-util'

Component({
  externalClasses: ['container-cls', 'cls', 'img-cls'],
  data: {
    in_src: '',
    cacheCode: 'img-'
  },
  properties: {
    code: {
      type: String,
      value: ''
    },
    src: {
      type: String,
      value: ''
    },
    type: {
      type: String,
      value: 'fill'
    },
    attachList: {
      type: Array,
      value: []
    },
    attachRefType: {
      type: String,
      value: ''
    },
    w: {
      type: Number,
      value: 100
    },
    h: {
      type: Number,
      value: 100
    },
    limit: {
      type: Number,
      value: 0
    },
    color: {
      type: String,
      value: 'ffffff'
    },
    cover: {
      type: Boolean,
      value: false
    }
  },
  created() {

  },
  observers: {
    'src, code, attachList': function () {
      this.init()
    }
  },
  lifetimes: {
    attached() {
      this.init()
    }
  },
  methods: {
    init() {
      const that = this
      // 在组件实例进入页面节点树时执行
      // console.log('attached', this.properties.code)
      if (this.properties.src) {
        that.setData({
          in_src: that.getImageUrl(this.properties.src)
        })
      } else if (this.properties.code) {
        this.findAttach(this.properties.code, (result) => {
          // console.log('attach is ', result)
          that.setData({
            in_src: that.getImageUrl(result.url)
          })
        })
      } else if (this.properties.attachList && this.properties.attachList.length > 0) {
        let img101 = ''
        let img104 = ''
        let imgUrl = ''
        const searchByType = (this.properties.attachRefType && this.properties.attachRefType !== '101')
        this.properties.attachList.forEach((atta) => {
          if (!img104 && atta.type === '104') {
            img104 = atta.attachUrl
          }
          if (!img101 && atta.type === '101') {
            img101 = atta.attachUrl
          }
          if (searchByType && !imgUrl && atta.type === this.properties.attachRefType) {
            imgUrl = atta.attachUrl
          }
        })
        imgUrl = img104 || imgUrl || img101
        that.setData({
          in_src: that.getImageUrl(imgUrl)
        })
      }
    },
    getImageUrl(url) {
      url += ((url.indexOf('?') === -1) ? '?' : '&') +
      'x-oss-process=image/resize,m_' + this.properties.type +
      ',w_' + this.properties.w * 2 +
      ',h_' + this.properties.h * 2 +
      ',limit_' + this.properties.limit +
      ',color_' + this.properties.color
      if (url.indexOf('http') === -1) { // 如果没有http开头，添加http开头
        if (url.startsWith('//')) {
          url = 'https:' + url
        } else {
          url = 'https://' + url
        }
      } else if (url.indexOf('http:') !== -1) { // 如果是http开头，修改成https开头
        url = url.replace('http:', 'https:')
      }
      url = url.replace('wdt-xilvxin.oss-cn-qingdao.aliyuncs.com', 'img.qingdaodujia.cn')
      url = encodeURI(url)
      // console.log('getImageUrl', url)
      return url
    },
    findAttach(code, callback) {
      // console.log('findAttach', code)
      if (!code) {
        return
      }
      if (code instanceof Array) {
        if (code.length === 0) {
          return
        }
        $http.post({
          url: 'attach/findByids',
          data: code,
          success: ({data}) => {
            callback(data.resultData)
          }
        })
      } else {
        const cacheKey = this.data.cacheCode + code
        const localData = wx.getStorageSync(cacheKey)
        if (localData) {
          // console.log('findAttach from localData', cacheKey)
          callback(localData)
        } else {
          $http.post({
            url: `attach/find/${code}`,
            success: ({data}) => {
              wx.setStorageSync(cacheKey, data.resultData)
              callback(data.resultData)
            }
          })
        }
      }
    }
  }
})
